﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PersonEntity
{
    public class DS_XML : iDS
    {
        private List<Person> pList = null;
        private PersonsEntities db;
        public DS_XML()
        {
            db = new PersonsEntities();
        }
        public override List<Person> Load()
        {
            //using (var context = new PersonsEntities())
            //{
            //    context.People.Load(); 
            //    var query = from b in context.People
            //                orderby b.FName
            //                select b;
            //    return query;
            //}
            return null;
        }

        public override void Delete(int ID)
        {
            Person found = null;
            foreach (Person pp in pList)
            {
                if (pp.Id == ID)
                {
                    found = pp;
                    break;
                }
            }

            if (found != null)
            {
                pList.Remove(found);
                Serializator.Serialization(this.path, pList);
            }
        }

        public override void Update(Person p)
        {
            Person found = null;
            foreach (Person pp in pList)
            {
                if (pp.Id == p.Id)
                {
                    found = pp;
                    break;
                }
            }

            if (found != null)
            {
                pList.Insert(pList.IndexOf(found), p);
                pList.Remove(found);
            }
            else
            {
                pList.Add(p);
            }

            Serializator.Serialization(this.path, pList);
        }

    }
}
